package com.github.postsanf.yinian.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.postsanf.yinian.BaseFragment;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.OfficialListAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.service.OfficialAlbumService;
import com.github.postsanf.yinian.utils.WrapContentStaggereGridLayoutManager;
import com.github.postsanf.yinian.view.SpacesItemDecoration;
import com.umeng.analytics.MobclickAgent;

public class NewStudentAlbumListFragment extends BaseFragment {
    private View view;                       // 上下文视图
    private RecyclerView rv_official_album;              // 列表视图
    private OfficialListAdapter mAdapter;                       // 适配器

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerBoradcastReceiver();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (YNSingleton.getInstance().getmNewStudentAlbumList() != null) {
            YNSingleton.getInstance().getmNewStudentAlbumList().clear();
        }
        noticeServiceInit();
        initView();
        return view;
    }

    private void initView() {
        view = View.inflate(getActivity(), R.layout.yn_fragment_officealbum, null);
        rv_official_album = (RecyclerView) view.findViewById(R.id.rv_official_album);
        //设置LayoutManager
        final WrapContentStaggereGridLayoutManager mManager = new WrapContentStaggereGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rv_official_album.setLayoutManager(mManager);
        // 设置适配器
        mAdapter = new OfficialListAdapter(getActivity(), YNSingleton.getInstance().getmNewStudentAlbumList());
        rv_official_album.setAdapter(mAdapter);
        //设置item之间的间隔
        SpacesItemDecoration decoration = new SpacesItemDecoration(8);
        rv_official_album.addItemDecoration(decoration);
    }

    // 分析并执行动作
    BroadcastReceiver mnewStudentListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                //　加载相册列表数据
                case ACTIONs.actionUpdateNewStudent:
                    mAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    // 注册接收者
    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionUpdateNewStudent);
        getActivity().registerReceiver(mnewStudentListReceiver, myIntentFilter);
    }

    // 通知后台加载数据
    private void noticeServiceInit() {
        if (getActivity() == null) {
            return;
        }
        Intent mIntent = new Intent(getActivity(), OfficialAlbumService.class);
        mIntent.setAction(ACTIONs.actionGetNewStudent);
        getActivity().startService(mIntent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getActivity().unregisterReceiver(mnewStudentListReceiver);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen");
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
    }
}